package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import Entity.AutocarsDBEntity;
import Entity.ManufacturesDBEntity;
import Entity.ManufacturyEntity;
import android.util.Xml;


public class XmlAutocarsParser {
	 private static final String ns = null;
	 public AutocarsDBEntity parse(InputStream in) throws XmlPullParserException, IOException {   
		 try {
         XmlPullParser parser = Xml.newPullParser();
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);       
         parser.setInput(in , null);
         parser.nextTag();       
         return readAutocarsXml(parser); 
	 } finally {
         in.close();
     }
 }
	 
	 private AutocarsDBEntity readAutocarsXml(XmlPullParser parser) throws XmlPullParserException, IOException {
		 parser.require(XmlPullParser.START_TAG, ns, "data");
		 AutocarsDBEntity autocars = new AutocarsDBEntity();
		 while (parser.next() != XmlPullParser.END_TAG) {
			 if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
			 String name = parser.getName();
			 if (name.equals("autocarinfos")) {
				 autocars.setAutocarinfos(readAutocarinfos(parser)) ;
				 
			 }
			 else if  (name.equals("autocars")) {
				 autocars.setAutocars(readAutocars(parser)) ;
				 
				 
			 }
	            
	            else{
	            	XmlTemplateParser.skip(parser);
	            }
		 }
		 return autocars;
	 }
	 
	 private ArrayList<Pair> readAutocarinfos(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<Pair> list= new ArrayList<Pair>();
	        parser.require(XmlPullParser.START_TAG, ns, "autocarinfos");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("autocarinfo")) {
	            	 list.add(readAutocarinfo(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 private OrderedPair<String, String> readAutocarinfo (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value = null ; 
		
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "autocarinfoname");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autocarinfoname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "autocarinfocode");     
	        value = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autocarinfocode");
	        parser.next();
	        parser.next();
	        OrderedPair <String, String> autocarinfo = new OrderedPair<String, String>(key,value);
		 return  autocarinfo;
	 }
	 
	 private ArrayList<MyFifthSet> readAutocars(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<MyFifthSet> list= new ArrayList<MyFifthSet>();
	        parser.require(XmlPullParser.START_TAG, ns, "autocars");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("autocar")) {
	            	 list.add(readAutocar(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 private OrderedFifthSet<String, String ,String ,String ,String> readAutocar (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 String value3 = null ; 
		 String value4 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "autoname");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autoname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "autocode");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autocode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "autounit");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autounit");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "autounitcode");     
	        value3 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autounitcode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "autoprice");     
	        value4 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "autoprice");
	        parser.next();
	        parser.next();
	        OrderedFifthSet <String, String, String , String , String> autocar = new  OrderedFifthSet<String, String, String ,String ,String>(key,value1,value2,value3,value4);
		 return  autocar;

	 }
	 
	 
	  
	
}
